<!--
 * @Author: weikang
 * @Description: 
 * @Date: 2023-12-19 10:26:14
 * @LastEditTime: 2023-12-22 17:17:56
 * @FilePath: \lianyungang-dashborad\src\model\warningAnalyze.vue
-->
<template>
  <div class="model-panel">
    <my-title title="预警分析"></my-title>
    <div class="content-panel">
      <div class="chart" ref="chartRef"></div>
    </div>
  </div>
</template>

<script setup>
import MyTitle from "@/components/MyTitle.vue";
import * as echarts from "echarts";
import { onMounted, ref, onUnmounted } from "vue";
import { getWarningAnalyze } from "@/api/apis";

const chartRef = ref();
const dataList = ref([]);
let myChart = null;
onMounted(() => {
  myChart = echarts.init(chartRef.value);
  myChart.setOption(getOptions());
  window.addEventListener("resize", () => {
    myChart.resize();
  });
});

onUnmounted(() => {
  window.removeEventListener("resize", () => {
    myChart.resize();
  });
});

const getOptions = () => {
  const option = {
    tooltip: {
      trigger: "item",
      className: "tooltip-analy",
      borderColor: "none",
      backgroundColor: "none",
      borderWidth: 0,
      textStyle: {
        color: "#fff",
      },
      axisPointer: {
        type: "shadow",
        textStyle: {
          color: "#fff",
        },
      },
    },
    grid: {
      borderWidth: 0,
      top: 30,
      bottom: 46,
      left: 80,
      right: 60,
      textStyle: {
        color: "#fff",
      },
    },
    legend: {
      icon: "circle",
      right: "4%",
      top: "0",
      textStyle: {
        color: "#FFFFFF",
      },
    },

    calculable: true,
    xAxis: [
      {
        type: "category",
        axisLine: {
          lineStyle: {
            color: "#90979c",
          },
        },
        splitLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        splitArea: {
          show: false,
        },
        axisLabel: {
          interval: 0,
          rotate: 25, // 倾斜度 -90 至 90，默认为0
        },
        data: dataList.value.map((item) => item.name),
      },
    ],
    yAxis: [
      {
        type: "value",
        splitLine: {
          show: false,
        },
        axisLine: {
          lineStyle: {
            color: "#90979c",
          },
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: "rgba(255,255,255,0.05)",
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          interval: 0,
        },
        splitArea: {
          show: false,
        },
      },
    ],
    series: [
      {
        name: "黄色预警",
        type: "bar",
        stack: "总量",
        barMinHeight: 20,
        itemStyle: {
          normal: {
            color: "#FEE500",
            label: {
              fontWeight: "bold",
              show: true,
              position: "inside",
            },
          },
        },
        data: dataList.value.map((item) => item.yellow),
      },
      {
        name: "橙色预警",
        type: "bar",
        stack: "总量",
        barMinHeight: 20,
        itemStyle: {
          normal: {
            color: "#D07B01",
            label: {
              show: true,
              color: "#fff",
              fontWeight: "bold",
              position: "inside",
            },
          },
        },
        data: dataList.value.map((item) => item.orange),
      },
      {
        name: "红色预警",
        type: "bar",
        stack: "总量",
        barGap: "10%",
        barMaxWidth: 35,
        barMinHeight: 20,
        barCategoryGap: "10%",
        itemStyle: {
          normal: {
            color: "#E60700",
            barBorderRadius: 0,
            label: {
              fontWeight: "bold",
              show: true,
              position: "inside",
            },
          },
        },
        data: dataList.value.map((item) => item.red),
      },
    ],
  };
  return option;
};

getWarningAnalyze().then((res) => {
  const {
    data: { data },
  } = res;
  console.log(
    "%c [ data ]-201",
    "font-size:13px; background:pink; color:#bf2c9f;",
    data
  );
  dataList.value = data;
  myChart.setOption(getOptions());
});
</script>

<style lang="scss" scoped>
.model-panel {
  height: 320px;
  .chart {
    height: 270px;
  }
}
</style>

<style>
.tooltip-analy {
  background: url("/src/assets/info.png") no-repeat;
  background-size: 100% 100%;
  color: #fff;
}
</style>